package co.com.jtune.model;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import co.com.jtune.libraries.BusinessException;

public class TrackModel {
	
	private Long id;
	private String name;
	private String artist;
	private String album;
	private Long duration;
	private String genre;
	
	private String location;
	
	public TrackModel(String name, String location)
	{
		this.name = name;
		this.location = location;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getLocation() {
		return location;
	}

	public void setLocation(String location) {
		this.location = location;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public String getArtist() {
		return artist;
	}

	public void setArtist(String artist) {
		this.artist = artist;
	}

	public String getAlbum() {
		return album;
	}

	public void setAlbum(String album) {
		this.album = album;
	}

	public Long getDuration() {
		return duration;
	}

	public void setDuration(Long duration) {
		this.duration = duration;
	}

	public String getGenre() {
		return genre;
	}

	public void setGenre(String genre) {
		this.genre = genre;
	}

	public boolean exists()
	{
		File trackFile = new File(location);
		return trackFile.exists();
	}

	public File getTrackFile() throws BusinessException
	{
		File track = new File(location);
		if (track.exists())
			return track;
		else
			throw new BusinessException("101");
	}
	
	public FileInputStream getTrackFileInputStream() throws BusinessException
	{
		try {
			return new FileInputStream(getTrackFile());
		} catch (FileNotFoundException e) {
			throw new BusinessException("101");
		}
	}
	
	public BufferedInputStream getTrackBufferedInputStream() throws BusinessException
	{
		return new BufferedInputStream(getTrackFileInputStream());
	}
	
	@Override
	public String toString() {
		return "Track [name=" + name + ", artist=" + artist + ", album="
				+ album + ", genre=" + genre + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((album == null) ? 0 : album.hashCode());
		result = prime * result + ((artist == null) ? 0 : artist.hashCode());
		result = prime * result + (int) (duration ^ (duration >>> 32));
		result = prime * result + ((genre == null) ? 0 : genre.hashCode());
		result = prime * result
				+ ((location == null) ? 0 : location.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		TrackModel other = (TrackModel) obj;
		if (album == null) {
			if (other.album != null)
				return false;
		} else if (!album.equals(other.album))
			return false;
		if (artist == null) {
			if (other.artist != null)
				return false;
		} else if (!artist.equals(other.artist))
			return false;
		if (duration != other.duration)
			return false;
		if (genre == null) {
			if (other.genre != null)
				return false;
		} else if (!genre.equals(other.genre))
			return false;
		if (location == null) {
			if (other.location != null)
				return false;
		} else if (!location.equals(other.location))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}
	
	
}
